<?php
  global $base_url;
  $modPath = drupal_get_path('module', 'e_district');
?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<!-- <script src="//code.jquery.com/jquery-1.9.1.js"></script>  -->
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<!-- Jquery table Grid -->
<script src="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css">

<script>
	$(function() {
			$( "#start_datepicker" ).datepicker({
			maxDate: new Date(),
			dateFormat: 'dd-mm-yy',
			showOn: "button",
			buttonImage: "<?php echo $modPath?>/image/calendar.gif",
			buttonImageOnly: true,
			onSelect: function(selected) {
				$("#end_datepicker").datepicker("option","minDate", selected)
			}

		});
		$( "#end_datepicker" ).datepicker({
			maxDate: new Date(),
			dateFormat: 'dd-mm-yy',
			showOn: "button",
			buttonImage: "<?php echo $modPath?>/image/calendar.gif",
			buttonImageOnly: true,
			onSelect: function(selected) {
				$("#start_datepicker").datepicker("option","maxDate", selected)
			}

		});

	});

	$(document).ready(function() {
		var from_date = $('#jq_from_date').val();
		var to_date = $('#jq_to_date').val();
	    $('#example').DataTable( {
	    	    "bLengthChange": false,
				"iDisplayLength": 50,
				"fnDrawCallback": function(oSettings) {
			        if ($('#example tr').length < 50) {
			            $('.dataTables_paginate').hide();
			        }
			    },
			    "footerCallback": function ( row, data, start, end, display ) {
		            var api = this.api(), data;

		            // Remove the formatting to get integer data for summation
		            var intVal = function ( i ) {
		                return typeof i === 'string' ?
		                    i.replace(/[\$,]/g, '')*1 :
		                    typeof i === 'number' ?
		                        i : 0;
		            };

		            // Total over all pages
		            total = api
		                .column( 2 )
		                .data()
		                .reduce( function (a, b) {
		                    return intVal(a) + intVal(b);
		                } );

		            // Total over this page
		            pageTotal = api
		                .column( 2, { page: 'current'} )
		                .data()
		                .reduce( function (a, b) {
		                    return intVal(a) + intVal(b);
		                }, 0 );

		            // Update footer
		            $( api.column( 2 ).footer() ).html(
		                'Total Transactions for the period ' + from_date + ' to ' + to_date + ' is ' +pageTotal
		            );
		        }
	    });
	} );

	function validateForm() {
	    var x = document.getElementById('start_datepicker').value;
	    var y = document.getElementById('end_datepicker').value;
	    if (x == null || x == "" || y == null || y == "") {
	        alert("Please choose the Date");
	        return false;
	    }
	}

</script>



<style>
.tableHeaderst{
    background-color: #ADDBF3;
	border:1px solid #2676BD;
    font-weight: bold;
}

.tableHeaderst1 {
border:1px solid #2676BD;
}
.form-submit {
    background-color: #5B74A8;
    border: 0px none;
    height: 22px;
    cursor: pointer;
    color: #FFF;
    font-weight: bold;
    font-family: Verdana,Arial,Helvetica,sans-serif;
}
table.dataTable thead th, table.dataTable thead td{
  padding: 9px;
}
</style>



  <div class="clear-block">
  <p style="color:#000099;font-weight:bold;font-size:16px;text-align:center;">e-Districts Total Cumulative</p>
  <form name="stat" id="stat" method="post" onsubmit="return validateForm()">
    <table width="618" border="0" style="text-align:center;">
      <tr>
        <td width="334">From:
          <input type="text" id="start_datepicker" size="15" name='start_datepicker' readonly="" /></td>
         <td width="334">To:
          <input type="text" id="end_datepicker" size="15" name='end_datepicker' readonly="" /></td>
    	<td width="274"><p> <input type="submit" id="val" size="30" name='val' value="get data" class="form-submit"/></p></td>

      </tr>
    </table>
  </form>


  <?php
if( ! ini_get('date.timezone') )
{
    date_default_timezone_set('GMT');
}

if(isset($_REQUEST['start_datepicker']) || isset($_REQUEST['end_datepicker'])){
  $start_date = "15-10-2010";
  $start = trim($_REQUEST['start_datepicker']);
  $end = trim($_REQUEST['end_datepicker']);
  $date_val_end = date('d/m/Y',strtotime($end));
  $cur_date  = date('d/m/Y');

  //echo strtotime($start); echo "&&&&&";
  //echo strtotime($cur_date); die;

  //$cur_date  = date('d/m/Y',strtotime("-1 days"));

  if(strtotime($start) < strtotime($start_date)){
  	$date_val_st = date('d/m/Y',strtotime($start_date));
  } else {
  	$date_val_st = date('d/m/Y',strtotime($start));
  }
  ?>
  <input type="hidden" value="<?php echo $date_val_st?>" id="jq_from_date">
  <input type="hidden" value="<?php echo $date_val_end?>" id="jq_to_date">
  <span><strong>Statistics as on date: <?php echo $date_val_st; ?>  To <?php echo $date_val_end; ?></strong></span> <?php

  /* 2. District-wise Total Cumulative figure on a given date */
  //$curl = curl_init('http://edistrict.tn.gov.in:8080/edistrict_tn_deity/getDistrictWiseCount/forGnfromDatetoDate/01/10/2014/29/10/2014');
  $curl = curl_init('http://164.100.167.34:8080/edistrict_tn_deity/getDistrictWiseCount/forGnfromDatetoDate/' .$date_val_st. '/' . $date_val_end );
  //$curl = curl_init('http://164.100.167.34:8080/edistrict_tn_deity/getDistrictWiseCount/forGnfromDatetoDate/01/12/2014/05/12/2014' );

  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_USERPWD, 'eportal:wyua2612');
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);


  $response = curl_exec($curl);
  $resultStatus = curl_getinfo($curl);
  $fp = fopen('statistics.xml', 'w');
  chmod("statistics.xml",0777);
  $xml = simplexml_load_string($response);

  //print_r($xml);
//print_r($xml); ?>
<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>S.No</th>
      <th>District Name</th>
      <th>Count</th>
      <th>Score (per 1000 population - based on census 2011)</th>
      <th>Rank</th>
    </tr>
  </thead>
  <tfoot>
            <tr>
                <th colspan="4" style="text-align:left">Total:</th>
                <th></th>
            </tr>
  </tfoot>
  <tbody>
<?php

if(count($xml)){
  foreach($xml as $data){ ?>

    <tr>
      <td><?php echo $data->slno;?></td>
      <td><?php echo $data->DistrictName;?></td>
      <td><?php echo $data->Count;?></td>
      <td><?php echo $data->RankScore;?></td>
      <td><?php echo $data->Rank;?></td>
    </tr>
  <?php } ?>
<?php } else{ ?>

 <tr>
    <td></td>
    <td></td>
    <td>Record not found</td>
    <td></td>
    <td></td>
  </tr>
  <?php } ?>

  </tbody>
</table>
<?php
curl_close($curl);
?>
<?php } ?>



